
######################
## PACKAGES
######################
library(dplyr)
library(haven)
library(foreign)
library(Hmisc)
library(ggplot2)

######################
## DATA
######################

Valg1965 <- read_dta("Downloads/VALGUNDERSOKELSENE/VAlgundersøkelsen 1965/NSD0054_F1.dta")
Valg1969 <- read_dta("Downloads/VALGUNDERSOKELSENE/Valgundersøkelsen 1969/NSD0055_F1.dta")
Valg1973 <- read_dta("Downloads/VALGUNDERSOKELSENE/Valgundersokelsen 1973/Folkeavstemningen om EF 1972 og Stortingsvalget 1973.dta")
Valg1977 <- read_dta("Downloads/VALGUNDERSOKELSENE/Valgundersokelsen 1977/Valgundersøkelsen 1977.dta")
Valg1981 <- read_dta("Downloads/VALGUNDERSOKELSENE/Valgundersokelsen 1981/Valgundersøkelsen 1981.dta")
Valg1985 <- read_dta("Downloads/VALGUNDERSOKELSENE/Valgundersokelsen 1985/Valgundersøkelsen 1985.dta")
Valg1989 <- read_dta("Downloads/VALGUNDERSOKELSENE/Valgundersokelsen 1989/Valgundersøkelsen 1989.dta")
Valg1993 <- read_dta("Downloads/VALGUNDERSOKELSENE/Valgundersokelsen 1993/Valgundersøkelsen 1993.dta")
Valg1997 <- read.dta("Downloads/VALGUNDERSOKELSENE/Valgundersokelsen 1997/Valgundersøkelsen 1997.dta") # Must use read.dta
Valg2001 <- read_dta("Downloads/VALGUNDERSOKELSENE/Valgundersokelsen 2001/Valgundersøkelsen 2001.dta")
Valg2005 <- read_dta("Downloads/VALGUNDERSOKELSENE/Valgunersokelsen 2005/Valgundersøkelsen 2005.dta")
Valg2009 <- read_dta("Downloads/VALGUNDERSOKELSENE/Valgundersokelsen 2009/Valgundersøkelsen 2009.dta")
Valg2013 <- read_dta("Downloads/VALGUNDERSOKELSENE/Valgundersokelsen 2013/Valgundersøkelsen 2013.dta")


################################## ELECTION STUDY 1965 ##################################
d <- Valg1965
# Harmonize election study data
table(d$v291)
d$income <- d$v291
table(d$v286)
d$education <- d$v286
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_1965 <- cov(d$score_inc,d$score_edu)



################################## ELECTION STUDY 1969 ##################################
d <- Valg1969
# Income and Education variables
table(d$v428)
d$income <- d$v428
table(d$v391)
d$education <- d$v391
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_1969 <- cov(d$score_inc,d$score_edu)


################################## ELECTION STUDY 1973 ##################################
d <- Valg1973
# Income and Education variables
table(d$v248)
d$income <- d$v248
table(d$v249)
d$education <- car::recode(d$v249,"9=NA")
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_1973 <- cov(d$score_inc,d$score_edu)


################################## ELECTION STUDY 1977 ##################################
d <- Valg1977
# Income and Education variables
table(d$v320)
d$income <- d$v320
table(d$v321)
d$education <- d$v321
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_1977 <- cov(d$score_inc,d$score_edu)


################################## ELECTION STUDY 1981 ##################################
d <- Valg1981
# Income and Education variables
table(d$var408)
d$income <- car::recode(d$var408,"8:hi=NA")
table(d$var397)
d$education <- car::recode(d$var397,"9:hi=NA")
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_1981 <- cov(d$score_inc,d$score_edu)



################################## ELECTION STUDY 1985 ##################################
d <- Valg1985
# Income and Education variables
table(d$income)
d$income <- car::recode(d$V486,"9:hi=NA")
table(d$V482)
d$education <- car::recode(d$V482,"9:hi=NA")
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_1985 <- cov(d$score_inc,d$score_edu)



################################## ELECTION STUDY 1989 ##################################
d <- Valg1989
# Income and Education variables
table(d$V534)
d$income <- car::recode(d$V534,"8:9=NA")
table(d$V528)
d$education <- car::recode(d$V528,"9:hi=NA")
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_1989 <- cov(d$score_inc,d$score_edu)



################################## ELECTION STUDY 1993 ##################################
d <- Valg1993
# Income and Education variables
table(d$v288)
d$income <- d$v288
table(d$education)
d$education <- car::recode(d$v285,"9=NA")
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_1993<- cov(d$score_inc,d$score_edu)


################################## ELECTION STUDY 1997 ##################################
d <- Valg1997
# Income and Education variables
table(d$v222)
d$income <- d$v222
table(d$education)
d$education <- d$v226
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_1997 <- cov(d$score_inc,d$score_edu)



################################## ELECTION STUDY 2001 ##################################
d <- Valg2001
# Income and Education variables
table(d$v461)
d$income <- car::recode(d$v461,"0=NA;999999:hi=NA")
table(d$v463)
d$education <- car::recode(d$v463,"9=NA")
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_2001<- cov(d$score_inc,d$score_edu)




################################## ELECTION STUDY 2005 ##################################
d <- Valg2005
# Income and Education variables
table(d$v294)
d$income <- car::recode(d$v294,"0=NA;99999998:hi=NA")
table(d$v287)
d$education <- car::recode(d$v287,"9=0; 98:hi=NA")
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_2005<- cov(d$score_inc,d$score_edu)





################################## ELECTION STUDY 2009 ##################################
d <- Valg2009
# Income and Education variables
table(d$v296)
d$income <- car::recode(d$v296,"0=NA;999999:hi=NA")
table(d$v295)
d$education <- car::recode(d$v295,"98:hi=NA")
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_2009<- cov(d$score_inc,d$score_edu)



################################## ELECTION STUDY 2013 ##################################
d <- Valg2013
# Income and Education variables
table(d$inntekt)
d$income <- d$inntekt
table(d$Bak28)
d$education <- car::recode(d$Bak28,"98:hi=NA")
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$income))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_inc = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_inc[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$income <- d1$Var1
d1 <- d1 %>% dplyr::select(income,score_inc)
# Apply to dataset
d <- merge(d,d1,by="income")
###########################
###########################
# Create midpoint score
d1 <- as.data.frame(table(d$education))
d1$prop <- d1$Freq/sum(d1$Freq, na.rm=T)
d1$cumu <- cumsum(d1$prop)
d1 <- mutate(d1, score_edu = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
d1$score_edu[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
d1$education <- d1$Var1
d1 <- d1 %>% dplyr::select(education,score_edu)
# Apply to dataset
d <- merge(d,d1,by="education")
###########################
cor(d$score_inc,d$score_edu,use="complete.obs")
cor(d$income,d$education,use="complete.obs")
cov(d$score_inc,d$score_edu)
##########################
cov_2013<- cov(d$score_inc,d$score_edu)


#######################################################################################
################################## COMBINE ############################################

covs <- c(cov_1965,cov_1969,cov_1973,cov_1977,cov_1981,cov_1985,
          cov_1989,cov_1993,cov_1997,cov_2001,cov_2005,cov_2009,cov_2013)
year <- c(seq(1965,2013,4))
covsdat <- data.frame(year,covs)
ggplot(covsdat,aes(year,covs)) +
  geom_smooth(method = "loess") +
  scale_y_continuous(limits=c(0,0.04))
m1 <- loess(covs~year,data=covsdat)
preds<-data.frame(year=seq(1966,2014,1))
preds$cov<-predict(m1,preds)
preds$cov[preds$year==2014] <- preds$cov[preds$year==2013]
covariances <- preds

